﻿@{
    ViewBag.Title = "源系统表列表";
    Layout = "~/Views/Shared/_Main.cshtml";
}
@section Head{
    <style>
        .div_collection, .mpedit
        {
            display: none;
            line-height: 25px;
        }
    </style>
    <script type="text/javascript">
        var clickAll = true;
        var UrlArray = $.getUrlArray();
        var retArr = new Array();
        var arrTr = new Array();
        var retData = new Array();
        var parent = window.dialogArguments; //获取父页面参数
        var extIDs = new Array();
        var parArr;
        if (parent && parent.retData) {
            parArr = parent.retData;
            for (var i = 0; i < parArr.length; i++) {
                extIDs.push(parArr[i].GUID);
            }
        }
        var opts = {
            rows: 10,
            height: 450,
            url: "/ODSConfig/GetStging_dbTbList?dbName=" + encodeURI(UrlArray["dbName"]),
            para: {},
            idField: "GUID",
            checkbox: true,
            columns: [
                    { field: 'DisplayName', title: '表名称', width: 30 },
                    { field: 'ViewChnName', title: '表说明', width: 40 },
                    { field: 'SourceSysName', title: '源系统名称', width: 30, formatter: function (value, rowData, rowIndex) {
                        var add = true; ;
                        for (var j = 0; j < retData.length; j++) {
                            if (retData[j].GUID == rowData.GUID) {
                                add = false;
                            }
                        }
                        if (add) {
                            retData.push(rowData);
                        }
                        arrTr[rowIndex] = value;
                        return value;
                    }
                    }
                    ],
            OnChickRow: function (value, rowData, rowIndex) {
                return clickRow(value, rowData, rowIndex, true);
            },
            OnLoaded: function () {
                var tr_c = "";
                var tr_v = "";
                if (arrTr.length > 0)
                    tr_v = arrTr[0];
                for (var i = 1; i < arrTr.length; i++) {
                    if (tr_v != arrTr[i]) {
                        tr_v = arrTr[i];
                        if (tr_c == "")
                            tr_c = "tr_c2";
                        else
                            tr_c = "";
                    }
                    $("#DataGrid tr:gt(" + i + ")").removeAttr("class");
                    $("#DataGrid tr:gt(" + i + ")").attr("class", tr_c);
                }
                return false;
            }
        };
        function clickRow(value, rowData, rowIndex, load) {
            if (UrlArray["mpedit"]) {
                if (event.srcElement.checked)
                    retArr.push(rowData.GUID);
                else {
                    retArr.removeValue(rowData.GUID);
                }
            }
            else {
                $("#sel_content").append("<span class='s1 s1" + value + "'><a title='" + rowData.ViewChnName + "'>" + rowData.DisplayName + "</a><span class='s2 s2" + value + "''>&nbsp;X&nbsp;</span></span>");
                $(".s2" + value).bind("click", function () {
                    $(".s1" + value).remove();
                    $(".s2" + value).remove();
                    for (var j = 0; j < retArr.length; j++) {
                        if (retArr[j].GUID == value) {
                            retArr.removeAt(j);
                            j--;
                        }
                    }
                    extIDs.removeValue(value);
                    if (retArr.length <= 1)
                        $(".div_collection").hide();
                    InitList();
                });

                retArr.push(rowData);
                extIDs.push(value);
                if (load) {
                    if (retArr.length > 1 && UrlArray["DBType"] == "2" && !UrlArray["tarviewID"])
                        $(".div_collection").show();

                    InitList();
                }
            }
            return true;

        }
        $(function () {
            if (UrlArray["mpedit"]) {
                $("#selected").hide();
                $(".mpedit").show();
            }
            InitList();
        });
        function InitList() {
            opts.para["viewID"] = UrlArray["viewID"];
            opts.para["tableName"] = $.trim($("#txt_TableName").val());
            opts.para["ExpIDs"] = extIDs.join(",");
            opts.para["sourceSysName"] = $.trim($("#txt_SourceSysName").val());
            opts.para["tarSysID"] = UrlArray["tarSysID"];
            $("#DataGrid").dg(opts);
        }
        function btnSearch() {
            thisPageNum = 1;
            InitList();
        }
        function flexiSelect() {
            if (retArr.length < 1) {
                alert('请选择一条数据!');
                return false;
            }
            var collType = "";
            if (UrlArray["DBType"] == "2" && retArr.length > 1)
                collType = $("input[name='collection']:checked").val();
            var retjson = { arr: retArr, collType: collType };
            returnParent(retjson);
            return false;
        }
        function remove() {
            if (confirm("确定要删除吗?")) {
                $.post("/EtlTarget/RemoveViewMapping", { sourceViewIDs: "'" + retArr.join("','") + "'", viewID: UrlArray["viewID"], dbName: encodeURI(UrlArray["dbName"]) }, function (res) {
                    if (res.Success) {
                        for (var i = 0; i < retArr.length; i++) {
                            for (var j = 0; j < retData.length; j++) {
                                if (retData[j].GUID == retArr[i]) {
                                    retData.removeAt(j);
                                    j--;
                                }
                            }
                        }
                        alert(res.Message);
                        InitList();
                    }
                    else
                    { alert(res.Message); }
                });
            }
        }
        function add() {
            showModalMany('add', '/ODSConfig/SourceList?tarSysID=' + UrlArray["tarSysID"] + "&tarviewID=" + UrlArray["viewID"] + '&dbName=' + encodeURI(UrlArray["dbName"] + "&DBType=2"), '900', '480', onSelectedCallBack);
        }

        function onSelectedCallBack(returnjson) {
            var returnValue = returnjson;
            var collType = "";
            var tarviewID = "";
            if (UrlArray["viewID"])
                tarviewID = UrlArray["viewID"];
            var sourID = new Array();
            if (returnjson.arr) {
                returnValue = returnjson.arr;
                collType = returnjson.collType;
            }
            for (var i = 0; i < returnValue.length; i++) {
                if (sourID.indexOf(returnValue[i].GUID) == -1)
                    sourID.push(returnValue[i].GUID);
            }
            $("#loading").show();
            if (sourID.length > 0) {
                $.post("/EtlTarget/AddTargetViewConfig?systemID=" + UrlArray["tarSysID"] + "&dbName=" + encodeURI(UrlArray["dbName"]),
                { SourceViewConfig: sourID.join(","), DBType: UrlArray["DBType"], collType: collType, tarviewID: tarviewID }, function (result) {
                    if (result == 1) {
                        alert('保存成功！');
                    } else if (result == 0) {
                        alert('保存失败！');
                    } else {
                        alert(result);
                    }
                    $("#loading").hide();
                    InitList();
                });
            } else
                $("#loading").hide();
        }
    </script>
}
<div style="height: 470px; padding-left: 5px; padding-right: 5px; overflow-y: scroll;">
    <table class="listTitle">
        <tr>
            <td width="15">
                <img src="/images/search.png" />
            </td>
            <td width="300">
                源系统表搜索
            </td>
            <td align="right">
            </td>
        </tr>
    </table>
    <table class="searchArea" width="100%">
        <tr>
            <td>
                表名称：
                <input type="text" id="txt_TableName" style="width: 200px;" />
                源系统名称：
                <input type="text" id="txt_SourceSysName" style="width: 200px;" />
                <input type="button" value="搜索" class="button" onclick="btnSearch()" />
            </td>
        </tr>
    </table>
    <table id="selected" style="width: 100%;">
        <tr>
            <td width="50">
                已选择：
            </td>
            <td>
                <div id="sel_content">
                </div>
            </td>
            <td width="50">
                <div class="div_collection">
                    <input type="radio" name='collection' value="1" checked="checked" />交集<input type="radio"
                        value='2' name='collection' />全集</div>
                <input type="button" value=" 确定 " class="button" onclick="flexiSelect()" />
            </td>
        </tr>
    </table>
    <table class="listTitle">
        <tr>
            <td width="15">
                <img src="/images/filesave.png" />
            </td>
            <td width="300">
                源系统表列表<span id="type_Name"></span>
            </td>
            <td align="right">
                <div class="mpedit">
                    <input type="button" value=" 添加 " class="button" onclick="add()" />
                    <input type="button" value=" 删除 " class="button" onclick="remove()" />
                </div>
            </td>
        </tr>
    </table>
    <table id="DataGrid" class="pagTable" cellpadding="0" cellspacing="0">
    </table>
    <div>
        <div id="Pagination" class="pagination">
        </div>
        <div id="loading" class="loading">
            <img src="/Images/tree_loading.gif" />
        </div>
    </div>
    <div id="msg">
    </div>
</div>
